package cn.iot.sys.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.iot.common.dto.ResultMsg;
import cn.iot.sys.dao.SysHostelDao;
import cn.iot.sys.entity.SysHostel;
import cn.iot.sys.service.SysHostelService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SysHostelServiceImpl implements SysHostelService {
    @Resource
    private SysHostelDao sysHostelDao;
    private Logger logger = LoggerFactory.getLogger(getClass());
    @Override
    public ResultMsg getSysHostelByDormitoryId(int dormitoryId) {
        logger.info("SysHostelServiceImpl getSysHostelByDormitoryId dormitoryId{}",dormitoryId);
        int count = sysHostelDao.getSysHostelByDormitoryId(dormitoryId);
        ResultMsg msg;
        if (count>0){
            msg = new ResultMsg(200,"成功",count);
            return msg;
        }
        msg = new ResultMsg(500,"失败",count);
        return msg;
    }

    @Override
    public ResultMsg getSysHostelList(Map<String, Object> map) {
        logger.info("SysHostelServiceImpl getSysHostelList map{}",map);
        int pageSize = Integer.parseInt(String.valueOf(map.get("pageSize")));
        int pageNum = Integer.parseInt(String.valueOf(map.get("pageNum")));
        String locationName = (String)map.get("locationName");
        if (StrUtil.isNotEmpty(locationName)){
            pageNum = 1;
            map.remove("pageNum");
            map.put("pageNum",pageNum);
        }
        int start = (pageNum-1)*pageSize;
        map.remove("pageSize");
        map.put("pageSize",pageSize);
        map.put("start",start);
        int count = sysHostelDao.getSysHostelCount(locationName);
        int pageCount;
        if (count>0){
            pageCount = count%pageSize==0? count/pageSize:count/pageSize+1;
        }else {
            pageCount = 0;
        }
        List<SysHostel> sysHostelList = sysHostelDao.getSysHostelList(map);
        map.put("sysHostelList",sysHostelList);
        map.put("pageCount",pageCount);
        ResultMsg msg = new ResultMsg(200,"成功",map);
        return msg;
    }

    @Override
    public ResultMsg getSysHostelCount(String locationName) {
        logger.info("SysHostelServiceImpl getSysHostelCount locationName{}",locationName);
        int count = sysHostelDao.getSysHostelCount(locationName);
        ResultMsg msg;
        if (count>0){
            msg = new ResultMsg(200,"成功",count);
            return msg;
        }
        msg = new ResultMsg(500,"失败",count);
        return msg;
    }

    @Transactional
    @Override
    public ResultMsg addSysHostel(SysHostel sysHostel) {
        logger.info("SysHostelServiceImpl addSysHostel sysHostel{}",sysHostel);
        ResultMsg msg;
        try {
            int count = sysHostelDao.addSysHostel(sysHostel);
            if (count>0){
                SysHostel sysHostelByMaxId = sysHostelDao.getSysHostelByMaxId();
                int bedCount = sysHostel.getBedCount();
                for (int i = 1;i<=bedCount;i++){
                    Map<String,Object> map = new HashMap<>();
                    map.put("dormitoryId",sysHostel.getDormitoryId());
                    map.put("describe","上铺");
                    map.put("hostelId",sysHostelByMaxId.getId());
                    map.put("bedNumber",i);
                    int num = sysHostelDao.addBed(map);
                    if (num <1){
                        int a = 9/0;
                    }
                }
                msg = new ResultMsg(200,"成功",count);
                return msg;
            }
        } catch (Exception e){
            throw new RuntimeException("异常回滚",e);
        }
        msg = new ResultMsg(500,"失败",null);
        return msg;
    }

    @Override
    public ResultMsg updSysHostelById(SysHostel sysHostel) {
        logger.info("SysHostelServiceImpl updSysHostelById sysHostel{}",sysHostel);
        ResultMsg msg;
        int isOccupyByHostelId = sysHostelDao.getIsOccupyByHostelId(sysHostel.getId());
        if (isOccupyByHostelId>0){
            msg = new ResultMsg(500,"该宿舍有人居住，不可修改",null);
            return msg;
        }
        int count = sysHostelDao.updSysHostelById(sysHostel);
        if (count>0){
            msg = new ResultMsg(200,"成功",count);
            return msg;
        }
        msg = new ResultMsg(500,"失败",count);
        return msg;
    }

    @Override
    public ResultMsg delSysHostelById(int id) {
        logger.info("SysHostelServiceImpl delSysHostelById id{}",id);
        ResultMsg msg;
        int isOccupyByHostelId = sysHostelDao.getIsOccupyByHostelId(id);
        if (isOccupyByHostelId>0){
            msg = new ResultMsg(500,"该宿舍有人居住，不可删除",null);
            return msg;
        }
        int count = sysHostelDao.delSysHostelById(id);
        if (count>0){
            msg = new ResultMsg(200,"成功",count);
            return msg;
        }
        msg = new ResultMsg(500,"失败",count);
        return msg;
    }

    @Override
    public ResultMsg getSysHostelAll() {
        logger.info("SysHostelServiceImpl getSysHostelAll start...");
        List<SysHostel> sysHostelAll = sysHostelDao.getSysHostelAll();
        ResultMsg msg = new ResultMsg(200,"成功",sysHostelAll);
        return msg;
    }
}
